﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class AddUpdateProduct : System.Web.UI.Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        //Setup CKEditor gọi CKFinder khi thêm Image
        CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
        _FileBrowser.BasePath = "AdminTools/ckfinder/";
        _FileBrowser.SetupCKEditor(ckeDescription);

        if (!Page.IsPostBack)
        {            
            load_RecordStatus();
            get_All_Category();
            Session["Image"] = null;

            if (Request.QueryString["id"] != null)
            {
                int iID = int.Parse(Request.QueryString["id"].ToString());
                load_Product(iID);
            }
        }
                
    }
    

    public void load_RecordStatus()
    {
        ListItem item = new ListItem();

        item.Value = "True";
        item.Text = "Công bố";
        item.Selected = true;        
        rdlRecordStatus.Items.Add(item);
            
        item = new ListItem();
        item.Value = "False";
        item.Text = "Chờ";
        rdlRecordStatus.Items.Add(item);
    }

    public void load_Product(int iID)
    {

        DataSet dsProduct = BusLogic.Product.get_PRODUCT(iID);
        if (dsProduct.Tables[0].Rows.Count > 0)
        {
            ddlCategory.SelectedValue = dsProduct.Tables[0].Rows[0]["CATID"].ToString();

            txtProductName.Text = dsProduct.Tables[0].Rows[0]["NAME"].ToString();
            txtPrice.Text = String.Format("{0:0.##}", Convert.ToDecimal(dsProduct.Tables[0].Rows[0]["PRICE"]));
            txtPromotionPrice.Text = String.Format("{0:0.##}", Convert.ToDecimal(dsProduct.Tables[0].Rows[0]["PROMOTION_PRICE"]));            
            
            rdlRecordStatus.SelectedValue = dsProduct.Tables[0].Rows[0]["RECORD_STATUS"].ToString();
            txtSorted.Text = dsProduct.Tables[0].Rows[0]["SORTED"].ToString();

            ckeDescription.Text = dsProduct.Tables[0].Rows[0]["DESCRIPTIONS"].ToString();

            if (!String.IsNullOrEmpty(dsProduct.Tables[0].Rows[0]["IMAGE"].ToString()))
            {
                trImageProduct.Visible = true;
                String[] sArrImage = dsProduct.Tables[0].Rows[0]["IMAGE"].ToString().Split(new Char[] { ';' });
                ArrayList arrImage = new ArrayList();

                for (int i = 0; i < sArrImage.Length; i++)
                {
                    if (!String.IsNullOrEmpty(sArrImage[i]))
                    { 
                        arrImage.Add(sArrImage[i]);
                    }                        
                }

                Session["Image"] = arrImage;

                repeaterImage.DataSource = arrImage;
                repeaterImage.DataBind();
            }
            
        }        
    }

    protected void lbtSave_Click(object sender, EventArgs e)
    {
        
            decimal dPrice = string.IsNullOrEmpty(txtPrice.Text) ? 0 : Convert.ToDecimal(txtPrice.Text, new System.Globalization.CultureInfo("en-US"));
            decimal dPromotionPrice = string.IsNullOrEmpty(txtPromotionPrice.Text) ? 0 : Convert.ToDecimal(txtPromotionPrice.Text, new System.Globalization.CultureInfo("en-US"));

            string sArrImage = "";
            if (Session["Image"] != null)
            {
                ArrayList arrImage = (ArrayList)Session["Image"];
                
                for (int i = 0; i < arrImage.Count; i++)
                {
                    sArrImage += arrImage[i].ToString() + ";";
                }
            }            

            if (Request.QueryString["id"] == null)
            {
                BusLogic.Product.add_PRODUCT(int.Parse(ddlCategory.SelectedValue.ToString()), txtProductName.Text, dPrice, dPromotionPrice, sArrImage, ckeDescription.Text, int.Parse(txtSorted.Text), Boolean.Parse(rdlRecordStatus.SelectedValue));
            }
            else
            {
                int iID = int.Parse(Request.QueryString["id"].ToString());
                BusLogic.Product.update_PRODUCT(iID, int.Parse(ddlCategory.SelectedValue.ToString()), txtProductName.Text, dPrice, dPromotionPrice, sArrImage, ckeDescription.Text, int.Parse(txtSorted.Text), Boolean.Parse(rdlRecordStatus.SelectedValue));
            }
            
            Response.Redirect("ProductManager.aspx");
    }

    public void load_Image()
    {
        if (Session["Image"] != null)
        {
            ArrayList arrImage = (ArrayList)Session["Image"];

            repeaterImage.DataSource = arrImage;
            repeaterImage.DataBind();
        }
    }

    protected void btAddImage_Click(object sender, EventArgs e)
    {
        if (Session["Image"] == null)
        {
            if (!String.IsNullOrEmpty(txtImage.Text))
            {
                ArrayList arrImage = new ArrayList();
                arrImage.Add(txtImage.Text);

                Session["Image"] = arrImage;
                load_Image();
            }
        }
        else
        {
            ArrayList arrImage = (ArrayList)Session["Image"];


            if (arrImage.Contains(txtImage.Text) == false && String.IsNullOrEmpty(txtImage.Text) != true)
            {
                arrImage.Add(txtImage.Text);
            }

            Session["Image"] = arrImage;
            load_Image();
        }
        
        if (!String.IsNullOrEmpty(txtImage.Text))
        {
            trImageProduct.Visible = true;
            txtImage.Text = "";
        }
        
    }

    protected void repeaterImage_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            string sImage = e.CommandArgument.ToString();

            ArrayList arrImage = (ArrayList)Session["Image"];

            arrImage.Remove(sImage);

            load_Image();
        }
    }

    public void get_All_Category()
    {
        ddlCategory.DataSource = BusLogic.Category.get_ALL_CATEGORY();
        ddlCategory.DataValueField = "ID";
        ddlCategory.DataTextField = "NAME";
        ddlCategory.DataBind();        
    }
}
